package com.xtgg.main;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.ImageLoaderHelper;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;
import com.xtgg.util.Utils;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.graphics.Color;

public class SearchDetailActivity extends BaseActivity implements OnClickListener {

	JSONObject mInfo;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_detail);

		mInfo = JSON.parseObject(getIntent().getStringExtra("baby"));

		((TextView) findViewById(R.id.center)).setText("寻人信息");
		((Button) findViewById(R.id.right)).setText("修改");
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));
		
		Date date = Utils.ConverToDate(mInfo.getString("birth"), "yyyy-MM-dd HH:mm");
		date = new Date(System.currentTimeMillis() - date.getTime());
		
		if(date.getYear() == 0)
		{
			((TextView)findViewById(R.id.age)).setText(date.getMonth() + "个月");
		}
		else
		{
			((TextView)findViewById(R.id.age)).setText(date.getYear() + "岁");
		}
		
		((TextView)findViewById(R.id.name)).setText(mInfo.getString("childname"));
		((TextView)findViewById(R.id.value0)).setText(mInfo.getString("misstime"));
		((EditText)findViewById(R.id.value1)).setText(mInfo.getString("missplace"));
		((EditText)findViewById(R.id.value2)).setText(mInfo.getString("description"));
		((EditText)findViewById(R.id.value3)).setText(mInfo.getString("remark"));
		((EditText)findViewById(R.id.value4)).setText(mInfo.getString("contacts"));
		((EditText)findViewById(R.id.value5)).setText(mInfo.getString("contacttel"));
		((EditText)findViewById(R.id.value6)).setText(mInfo.getString("contactaddress"));

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.action0).setOnClickListener(this);
		findViewById(R.id.img0).setOnClickListener(this);
		findViewById(R.id.img1).setOnClickListener(this);
		findViewById(R.id.img2).setOnClickListener(this);
		
		JSONArray list = mInfo.getJSONArray("infopic");
		for(int i = 0; i < list.size() && i < 3; i++)
		{
			ImageLoaderHelper.displayImage(R.drawable.img_default,
					((ImageView)findViewById(R.id.img0 + 2*i)), list.getJSONObject(i).getString("path"),
					20);
			findViewById(R.id.img0 + 2*i).setTag(list.getJSONObject(i).getString("path"));
		}
		
		if(list.size() == 0)
		{
			findViewById(R.id.layout).setVisibility(View.GONE);
			findViewById(R.id.line).setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.right:
			sendHttpRequest();
			break;

		case R.id.action0: {
			final Calendar c = Calendar.getInstance();
			int mYear = c.get(Calendar.YEAR);
			int mMonth = c.get(Calendar.MONTH);
			int mDay = c.get(Calendar.DAY_OF_MONTH);
			new DatePickerDialog(this, new OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker arg0, int arg1, int arg2,
						int arg3) {
					StringBuffer buffer = new StringBuffer();
					buffer.append(arg1);
					buffer.append("-");
					arg2++;
					if (arg2 < 10)
						buffer.append(0);
					buffer.append(arg2);
					buffer.append("-");
					if (arg3 < 10)
						buffer.append(0);
					buffer.append(arg3);

					((TextView) findViewById(R.id.value0)).setText(buffer
							.toString());
				}
			}, mYear, mMonth, mDay).show();
			break;
		}

		case R.id.img0:
		case R.id.img1:
		case R.id.img2:
		{
			Intent intent = new Intent(this,SingleImageShowActivity.class);
			intent.putExtra("url", (String)arg0.getTag());
			startActivity(intent);
		}
			break;
		}
	}

	private void sendHttpRequest() {
		String place = ((EditText) findViewById(R.id.value6)).getText()
				.toString();
		String time = ((TextView) findViewById(R.id.value5)).getText()
				.toString();
		if (place.length() == 0 || time.length() == 0) {
			ToastUtil.showToastWithAlertPic("请输入失踪时间&失踪地点");
			return;
		}

		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					ToastUtil.showToastWithAlertPic("寻人信息发布成功");
					finish();
				} 
				else if (ruser.getState().equals("2")) 
				{showExitDialog();} 
				else {
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};

		RequestParams params = new RequestParams();
		params.put("act", "operateInfo");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		params.put("type", "0");
		params.put("infoid", mInfo.getString("infoid"));
		params.put("childname", ((TextView) findViewById(R.id.name))
				.getText().toString());
		params.put("info_sex", mInfo.getString("sex"));
		params.put("birth", mInfo.getString("birth"));
		params.put("misstime", ((TextView) findViewById(R.id.value0)).getText()
				.toString());
		params.put("missplace", ((EditText) findViewById(R.id.value1))
				.getText().toString());
		params.put("description", ((EditText) findViewById(R.id.value2))
				.getText().toString());
		params.put("remark", ((EditText) findViewById(R.id.value3)).getText()
				.toString());
		params.put("contacts", ((EditText) findViewById(R.id.value4)).getText()
				.toString());
		params.put("contacttel", ((EditText) findViewById(R.id.value5))
				.getText().toString());
		params.put("contactaddress", ((EditText) findViewById(R.id.value6))
				.getText().toString());

		MgqRestClient.get(Constants.INFOMGT, params, loginHandler);
	}
}
